package servlet;

import dao.BookDAO;
import pojo.Book;
import utils.ApiResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/books")
public class BookServlet extends BaseServlet {

    // 查询所有图书
    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            List<Book> books = BookDAO.getAllBooks();
            sendJsonResponse(resp, new ApiResult(200, "查询所有图书成功", books));
        } catch (SQLException e) {
            sendJsonResponse(resp, new ApiResult(500, "查询所有图书失败", e.getMessage()));
        }
    }

    // 新增图书
    public void addBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String title = req.getParameter("title");
        String author = req.getParameter("author");
        String isbn = req.getParameter("isbn");
        String coverImageUrl = req.getParameter("coverImageUrl");
        double price = Double.parseDouble(req.getParameter("price"));

        Book book = new Book();
        book.setTitle(title);
        book.setAuthor(author);
        book.setIsbn(isbn);
        book.setCoverImageUrl(coverImageUrl);
        book.setPrice(price);

        try {
            BookDAO.addBook(book);
            sendJsonResponse(resp, new ApiResult(200, "新增图书成功", null));
        } catch (SQLException e) {
            sendJsonResponse(resp, new ApiResult(500, "新增图书失败", e.getMessage()));
        }
    }

    // 更新图书信息
    public void updateBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int bookId = Integer.parseInt(req.getParameter("bookId"));
        String title = req.getParameter("title");
        String author = req.getParameter("author");
        String isbn = req.getParameter("isbn");
        String coverImageUrl = req.getParameter("coverImageUrl");
        double price = Double.parseDouble(req.getParameter("price"));

        Book book = new Book();
        book.setBookId(bookId); // 注意要设置图书ID
        book.setTitle(title);
        book.setAuthor(author);
        book.setIsbn(isbn);
        book.setCoverImageUrl(coverImageUrl);
        book.setPrice(price);

        try {
            BookDAO.updateBook(book);
            sendJsonResponse(resp, new ApiResult(200, "图书更新成功", null));
        } catch (SQLException e) {
            sendJsonResponse(resp, new ApiResult(500, "图书更新失败", e.getMessage()));
        }
    }

    // 删除图书
    public void deleteBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int bookId = Integer.parseInt(req.getParameter("bookId"));

        try {
            BookDAO.deleteBook(bookId);
            sendJsonResponse(resp, new ApiResult(200, "图书删除成功", null));
        } catch (SQLException e) {
            sendJsonResponse(resp, new ApiResult(500, "图书删除失败", e.getMessage()));
        }
    }
}
